# Maintainer: Antoine Martin <totaam@xpra.org>

_realname=xpra
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-common"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.0.1
pkgrel=1
pkgdesc="Remote access client / server software"
arch=('any')
url='https://xpra.org'
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-libyuv"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-x264-git"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo")
optdepends=("${MINGW_PACKAGE_PREFIX}-libnotify: notification support"
            "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad: extra audio codecs"
            "${MINGW_PACKAGE_PREFIX}-gst-plugins-ugly: extra audio codecs"
            "${MINGW_PACKAGE_PREFIX}-gst-plugins-good: extra audio codecs"
            "${MINGW_PACKAGE_PREFIX}-gst-python: audio support")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             #python2 build deps:
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython2"
             "${MINGW_PACKAGE_PREFIX}-pygobject2-devel"
             "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
             #python3 build deps:
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-pygobject-devel"
             "${MINGW_PACKAGE_PREFIX}-python3-cairo")
source=("https://xpra.org/src/xpra-${pkgver}.tar.xz")
sha512sums=('2fd04afe416c9b26c5404f0114c8abbe8367a6cb3e745ffaecedcbb864b1ada4575f7289807a152f6d01834e1f1fc4d6ac780335537f69ec393ef3d16928f4e2')

prepare() {
  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build \
    --without-cuda_kernels --without-data  --without-html5 --with-modules
  done
}

package_python3-xpra() {
  depends+=("${MINGW_PACKAGE_PREFIX}-${_realname}-common"
            "${MINGW_PACKAGE_PREFIX}-python3"
            "${MINGW_PACKAGE_PREFIX}-gtk3"
            "${MINGW_PACKAGE_PREFIX}-python3-lz4"
            "${MINGW_PACKAGE_PREFIX}-python3-rencode"
            "${MINGW_PACKAGE_PREFIX}-python3-pillow"
            "${MINGW_PACKAGE_PREFIX}-python3-PyOpenGL"
            "${MINGW_PACKAGE_PREFIX}-python3-comtypes"
            "${MINGW_PACKAGE_PREFIX}-python3-setproctitle")
  optdepends+=("${MINGW_PACKAGE_PREFIX}-python3-paramiko: SSH client and server support"
              "${MINGW_PACKAGE_PREFIX}-python3-dnspython: SSHFP support with paramiko"
              "${MINGW_PACKAGE_PREFIX}-python3-netifaces: mDNS and network integration"
              "${MINGW_PACKAGE_PREFIX}-python3-pyu2f: U2F authentication"
              "${MINGW_PACKAGE_PREFIX}-python3-ldap: LDAP authentication via python-ldap"
              "${MINGW_PACKAGE_PREFIX}-python3-ldap3: LDAP authentication via python-ldap3"
              "${MINGW_PACKAGE_PREFIX}-python3-cryptography: AES packet encryption"
              "${MINGW_PACKAGE_PREFIX}-python3-zeroconf: mDNS support"
              "${MINGW_PACKAGE_PREFIX}-python3-nvidia-ml: nvidia GPU support"
              )

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --without-cuda_kernels --without-data  --without-html5 --skip-build \
    --root="${pkgdir}/${MINW_PREFIX}" --share-xpra="./share/xpra"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_python2-xpra() {
  depends+=("${MINGW_PACKAGE_PREFIX}-${_realname}-common"
            "${MINGW_PACKAGE_PREFIX}-python2"
            "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
            "${MINGW_PACKAGE_PREFIX}-python2-gobject"
            "${MINGW_PACKAGE_PREFIX}-python2-lz4"
            "${MINGW_PACKAGE_PREFIX}-python2-rencode"
            "${MINGW_PACKAGE_PREFIX}-python2-pillow"
            "${MINGW_PACKAGE_PREFIX}-python2-PyOpenGL"
            "${MINGW_PACKAGE_PREFIX}-python2-comtypes"
            "${MINGW_PACKAGE_PREFIX}-python2-setproctitle"
            "${MINGW_PACKAGE_PREFIX}-python2-zeroconf")
  optdepends+=("${MINGW_PACKAGE_PREFIX}-python2-paramiko: SSH client and server support"
              "${MINGW_PACKAGE_PREFIX}-python2-dnspython: SSHFP support with paramiko"
              "${MINGW_PACKAGE_PREFIX}-python2-netifaces: mDNS and network integration"
              "${MINGW_PACKAGE_PREFIX}-python2-pyu2f: U2F authentication"
              "${MINGW_PACKAGE_PREFIX}-python2-ldap: LDAP authentication via python-ldap"
              "${MINGW_PACKAGE_PREFIX}-python2-ldap3: LDAP authentication via python-ldap3"
              "${MINGW_PACKAGE_PREFIX}-python2-cryptography: AES packet encryption"
              "${MINGW_PACKAGE_PREFIX}-python2-nvidia-ml: nvidia GPU support"
              )

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --without-cuda_kernels --without-data  --without-html5 --skip-build \
    --root="${pkgdir}/${MINW_PREFIX}" --share-xpra="${pkgdir}/${MINGW_PREFIX}/share/xpra"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_xpra-common() {
  depends=()
  optdepends=()
  makedepends=("${MINGW_PACKAGE_PREFIX}-brotli")
  cd "${srcdir}/${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/python2 setup.py install --skip-build --minimal --with-data \
    --home="${pkgdir}/${MINGW_PREFIX}" --root=""

  SHARE_DIR="${pkgdir}${MINGW_PREFIX}/share"
  APP_SHARE_DIR="${SHARE_DIR}/${_realname}"
  install -Dm644 COPYING "${SHARE_DIR}/licenses/${_realname}/COPYING"
  install -Dm644 README "${APP_SHARE_DIR}/README"
  install -Dm644 bell.wav "${APP_SHARE_DIR}/bell.wav"

  #for nvenc builds only:
  #install -Dm644 dist/etc/${_realname}/*.keys "${APP_ETC}/"
  #install -Dm644 dist/etc/${_realname}/cuda.conf "${APP_ETC}/${conffile}"

  #python version agnostic launch scripts in /bin:
  for script in xpra xpra_launcher; do
    install -Dm755 scripts/${script} "${pkgdir}${MINGW_PREFIX}/bin/${script}"
  done

  #html5 client:
  ${MINGW_PREFIX}/bin/python2 ./setup_html5.py "${pkgdir}${MINGW_PREFIX}/share/${_realname}/www/"

  #this is not a python installation package,
  #so remove the python crud (all empty anyway):
  rm ${pkgdir}/${MINGW_PREFIX}/lib/python/xpra*.egg-info
  rmdir "${pkgdir}/${MINGW_PREFIX}/lib/python"
  rmdir "${pkgdir}/${MINGW_PREFIX}/lib"
}


package_mingw-w64-i686-xpra-common() {
  package_xpra-common
}

package_mingw-w64-i686-python2-xpra() {
  package_python2-xpra
}

package_mingw-w64-i686-python3-xpra() {
  package_python3-xpra
}

package_mingw-w64-x86_64-xpra-common() {
  package_xpra-common
}

package_mingw-w64-x86_64-python2-xpra() {
  package_python2-xpra
}

package_mingw-w64-x86_64-python3-xpra() {
  package_python3-xpra
}
